package com.ld.security.shop.crawler.modules.wo.service.impl;

import com.ld.security.common.core.base.column.MapJson;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.core.util.TemplateUtil;
import com.ld.security.common.core.util.UuidUtil;
import com.ld.security.common.mybatis.base.po.IdPo;
import com.ld.security.common.mybatis.base.po.IdsPo;
import com.ld.security.common.quartz.modules.job.constant.JobTaskConstant;
import com.ld.security.common.quartz.modules.job.entity.JobTask;
import com.ld.security.common.quartz.modules.job.service.IJobTaskService;
import com.ld.security.common.tomcat.util.AppUtil;
import com.ld.security.shop.crawler.modules.wo.constant.WoGroupConstant;
import com.ld.security.shop.crawler.modules.wo.constant.WoTaskConstant;
import com.ld.security.shop.crawler.modules.wo.entity.WoTask;
import com.ld.security.shop.crawler.modules.wo.job.WoGroupJob;
import com.ld.security.shop.crawler.modules.wo.service.IWoGroupService;
import com.ld.security.shop.crawler.modules.wo.entity.WoGroup;
import com.ld.security.common.mybatis.base.service.*;
import com.ld.security.shop.crawler.modules.wo.service.IWoTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
class WoGroupServiceImpl extends ISuperServiceImpl<WoGroup> implements
        IWoGroupService {

    @Autowired
    IWoTaskService iWoTaskService;

    @Autowired
    IJobTaskService jobTaskService;


    Set<String> toSet(String... ids) {
        HashSet<String> set = new HashSet<>();
        for (String x : ids) {
            set.add(x);
        }
        return set;
    }



    private void saveJob(WoGroup entity){
        switch (entity.getJobType()){
            case clock_task:{
                //创建定时任务
                JobTask jobTask =  jobTaskService.newInstance();
                jobTask.setAppName(AppUtil.getAppName());
                jobTask.setJobGroup(this.getJobGroupName());
                jobTask.setJobName(entity.getLabel());
                jobTask.setJobStatus(JobTaskConstant.jobStatus.run);
                jobTask.setCronExpression(entity.getScheduled());
                MapJson mapJson =  new MapJson();
                mapJson.put("woGroupId",entity.getId());
                jobTask.setDataMap(mapJson);
                jobTask.setJobClassName(WoGroupJob.class.getName());
                jobTaskService.save(jobTask);
                break;
            }
            case simp_task:{
                //定时任务删除
                JobTask jobTask =  jobTaskService.newInstance();
                jobTask.setAppName(AppUtil.getAppName());
                jobTask.setJobGroup(this.getJobGroupName());
                jobTask.setJobName(entity.getLabel());
                UuidUtil.initIdKey(jobTask);
                IdPo po = new IdPo(jobTask.getId());
                jobTaskService.remove(po);
                break;
            }
        }

    }



    @Override
    public int save(WoGroup entity) {


        String content = StringUtil.removeLine(entity.getContent());
        if (!TemplateUtil.isNull(entity.getId())) {

            WoGroup e = this.getById(entity.getId());
            if (e != null && !e.getContent().equals(entity.getContent())) {
                String[] xs = e.getContent().split(",");
                String[] ys = entity.getContent().split(",");
                Set<String> set = this.toSet(ys);

                List<String> ids = Arrays.asList(xs).stream().filter(val -> {
                    return !set.contains(val);
                })
                        .map(val -> UuidUtil.getId(val))
                        .collect(Collectors.toList());

                IdsPo po = new IdsPo(ids);
                iWoTaskService.remove(po);
            }

            //发现地址变化,状态变化
            entity.setStatus(WoGroupConstant.status.setout);

        }else{
            entity.setId(UuidUtil.getId(entity.getContent()));
        }
        int total = super.save(entity);
        for (String s : content.split(",")) {
            WoTask woTask = iWoTaskService.newInstance();
            woTask.setStatus(WoTaskConstant.status.await);
            woTask.setUrl(s);
            woTask.setLabel(entity.getLabel() + ":" + s);
            woTask.setGroupId(entity.getId());
            woTask.putJsonKey("groupId",entity.getLabel());
            iWoTaskService.parseSave(woTask);
        }
        this.saveJob(entity);

        return total;
    }

    @Override
    public synchronized int parseSave(WoGroup woGroup) {
        woGroup.setId(UuidUtil.getId(woGroup.getContent()));
        if(this.count(new IdPo(woGroup.getId())) == 0){
           return this.save(woGroup);
        }
        return 0;
    }

    @Override
    public boolean updateStatus(String id, WoGroupConstant.status status) {
        WoGroup woGroup =  this.newInstance();
        woGroup.setId(id);
        woGroup.setStatus(status);
        return super.save(woGroup) > 0;
    }

    private String getJobGroupName(){
        return this.getEntityClass().getSimpleName();
    }

}
